package net.sppan.blog.handler;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sppan.blog.model.Res;
import net.sppan.blog.model.Role;
import net.sppan.blog.model.User;
import net.sppan.blog.utils.WebUtils;

import com.jfinal.handler.Handler;

public class SomethingHandler extends Handler {

	@Override
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		if (target.startsWith("/admin")) {
			//管理后台用户信息
			User user = WebUtils.currentUser(request, response);
			if (user != null && user.getId() != null) {
				request.setAttribute("currentUser", user);
				Role role = (Role) request.getSession().getAttribute("loginUserRole");
				if (role == null) {
					role = Role.dao.findRoleByUserId(user.getId());
					request.getSession().setAttribute("loginUserRole", role);
				}
				@SuppressWarnings("unchecked")
				List<Res> list = (List<Res>) request.getSession().getAttribute("menu");
				if (list == null) {
					List<Res> res = Res.dao.getResourceList(user.getId(), null);
					request.getSession().setAttribute("menu", res);
				}
			}
		} else if (target.contains("error")) {
			target = "error.html";
		}
		next.handle(target, request, response, isHandled);
	}

}
